<?php
namespace Budget\BudgetBundle\Services;

use Budget\BudgetBundle\Entity\Family;
use Doctrine\ORM\EntityManager;
use Budget\BudgetBundle\Helper\DateHelper;
use Budget\BudgetBundle\Helper\FamilyHelper;

class IncomeService
{
    protected $entityManager;
    
    public function __construct(EntityManager $entityManager) {
        $this->entityManager = $entityManager;
    }
    
    public function getIncomesForFamilyToArray(Family $family, $page, $limit, $sidx, $sord, $dateTo = null, $dateFrom = null)
    {
        if (!$sidx) {
            $sidx = 1;
        }
        
        if (is_null($dateFrom)) {
            $dateFrom = new \DateTime();
            $dateFrom->setDate($dateFrom->format('Y'), 1, 1);
            $dateFrom = $dateFrom->format('m.Y');
        }
        
        $dateFrom = FamilyHelper::getStartMonth($family, $dateFrom);
        $dateTo   = FamilyHelper::getEndMonth($family, $dateTo);
                       
        $qb = $this->entityManager->getRepository('BudgetBudgetBundle:Income')
                   ->getIncomesForFamilyToArrayQuery($family, $dateTo, $dateFrom, $sidx, $sord);
 
        return  $qb->getQuery()->getArrayResult();  
    }
    
    public function getTotalSumIncomesForFamily(Family $family, $dateTo = null, $dateFrom = null)
    {
        $dateFrom = FamilyHelper::getStartMonth($family, $dateFrom);
        $dateTo   = FamilyHelper::getEndMonth($family, $dateTo);

        $qb = $this->entityManager->getRepository('BudgetBudgetBundle:Income')
                ->getTotalSumIncomesForFamilyQuery($family, $dateTo, $dateFrom);
        
        return  $qb->getQuery()->getSingleResult();  
    }
    
    public function getFormatIncomesToJson($incomes)
    {       
        $response            = array();
        $response['page']    = 1;
        $response['total']   = 1;
        $response['records'] = count($incomes);

        foreach ($incomes AS $income) {  
            $response['rows'][] = array(
                'id' => $income['id'], 
                'cell' => array(
                    $income['id'],
                    $income['amount'],
                    $income['comment'],
                    $income['date_created']->format(DateHelper::FORMAT_USER)
                )
             );
        }
        return $response;
    }
    
    public function getFormatIncomesToChartJson($incomes, $dateTo, $dateFrom)
    {       
        $months = $response = array();
        if ($dateTo >= $dateFrom) {
            $arrayDateFrom = explode('-', $dateFrom);
            $i  = 0;
            $mm = $arrayDateFrom[1];
            $yy = $arrayDateFrom[0];
            while (true) {
                $months[$i]   = $yy.'-'.$mm;
                $response[$i] = array(DateHelper::getMonthName($mm), 0);
                $i++;
                if ($yy.'-'.$mm == $dateTo) {
                    break;
                } else {
                    if ($mm == 12) {
                       $mm  = 1;
                       $yy += 1;
                    } else {
                        $mm += 1;
                    }
                    if($mm < 10) {
                        $mm = '0'.$mm;
                    }
                }
            }
        }
        
        $count = count($months);
        foreach ($incomes AS $income) {  
            $month = $income['date_created']->format(DateHelper::FORMAT_YEAR_MONTH); 
            for($i=0; $i<$count; $i++) {
                if($months[$i] == $month) {
                    break;
                }
            }
            $response[$i][1] = $response[$i][1] + $income['amount'];
        }
        return $response;
    }
}

